<?php

include_once($ROOT . "base.inc.php");

class PageController extends BaseController {
  var $user;
  var $requestedPage;

  function PageController() {
  	parent::__construct();
	$this->title = "Login";
	$this->section = "none";

	$action = $this->getParameter("action");
	$this->user = new User();

	switch (strtoupper($action))
	{
		case "LOGIN" :
		  $this->parseForm();
		  if (!$this->validate()) return;

		  if (!UserUtil::authenticate($this->user->username, $this->user->password))
		  {
		  	  $this->addError("Unable to authenticate");
		  	  return;
		  }

		  header('Location: index.php?page=' . $this->requestedPage);
		  exit;

		  break;
		default :
	}
  }

  function parseForm() {
	$this->user->readForm();

	$this->requestedPage = "index";
	$reqPage = $this->getParameter("requestedPage");
	if ($reqPage != "" && $reqPage != "login")
	{
		$this->requestedPage = $reqPage;
	}
  }

  function validate() {
  	if (strlen($this->user->username) == 0)
  	{
  		$this->addError("Username is Required");
  	}
  	if (strlen($this->user->password) < 4)
  	{
  		$this->addError("Password must be at least 4 characters");
  	}
  	return (sizeof($this->errs) == 0);
  }

}



?>
